package com.hrrt.home.admin.controller.manage;

import com.baidu.unbiz.fluentvalidator.ComplexResult;
import com.baidu.unbiz.fluentvalidator.FluentValidator;
import com.baidu.unbiz.fluentvalidator.ResultCollectors;
import com.hrrt.common.base.BaseController;
import com.hrrt.common.validator.LengthValidator;
import com.hrrt.home.common.constant.HomeResult;
import com.hrrt.home.common.constant.HomeResultConstant;
import com.hrrt.home.dao.model.*;
import com.hrrt.home.rpc.api.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 2017-9-05 jiangyanpeng
 * 设备管理
 */
@Controller
@Api(value = "设备管理", description = "设备管理")
@RequestMapping("/manage/equipment")
public class HomeEquipmentController extends BaseController {
    private static Logger _log = LoggerFactory.getLogger(HomeEquipmentController.class);

    @Autowired
    private HomeEquipmentService homeEquipmentService;
    @Autowired
    HomeBaseDeviceService homeBaseDeviceService ;
    @Autowired
    HomeRoomService homeRoomService;
    @Autowired
    HomeDeviceStatusService homeDeviceStatusService;

    @ApiOperation(value = "设备首页")
    @RequiresPermissions("home:equipment:read")
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index() {
        return "/manage/equipment/index.jsp";
    }

    @ApiOperation(value = "设备列表")
    @RequiresPermissions("home:equipment:read")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public Object list(
            @RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            @RequestParam(required = false, value = "sort") String sort,
            @RequestParam(required = false, value = "order") String order) {
        HomeEquipmentExample homeEquipmentExample = new HomeEquipmentExample();
        if (!StringUtils.isBlank(sort) && !StringUtils.isBlank(order)) {
            homeEquipmentExample.setOrderByClause(sort + " " + order);
        }
        List<HomeEquipment> rows = homeEquipmentService.selectByExampleForOffsetPage(homeEquipmentExample, offset, limit);
        long total = homeEquipmentService.countByExample(homeEquipmentExample);

        for(HomeEquipment list : rows){

            String romeName = homeRoomService.selectByPrimaryKey(list.getRoomId()).getName();
            String equipName = homeBaseDeviceService.selectByPrimaryKey(list.getEquipBasicId()).getText();
            list.setRoomName(romeName);
            list.setEquipName(equipName);

        }

        Map<String, Object> result = new HashMap<>();
        result.put("rows", rows);
        result.put("total", total);
        return result;
    }

    @ApiOperation(value = "新增设备")
    @RequiresPermissions("home:equipment:create")
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String create(ModelMap modelMap) {

        HomeRoomExample homeRoomExample = new HomeRoomExample();
        List<HomeRoom> homeRooms = homeRoomService.selectByExample(homeRoomExample);
        modelMap.put("homeRooms",homeRooms);

        HomeBaseDeviceExample homeBaseDeviceExample = new HomeBaseDeviceExample();
        List<HomeBaseDevice> homeBaseDevices = homeBaseDeviceService.selectByExample(homeBaseDeviceExample);
        modelMap.put("homeBaseDevices",homeBaseDevices);

        HomeEquipmentExample homeEquipmentExample = new HomeEquipmentExample();
        List<HomeEquipment> homeEquipments = homeEquipmentService.selectByExample(homeEquipmentExample);
        modelMap.put("homeEquipments", homeEquipments);
        return "/manage/equipment/create.jsp";
    }

    @ApiOperation(value = "新增设备")
    @RequiresPermissions("home:equipment:create")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public Object create(HomeEquipment homeEquipment) {
        homeEquipment.setLastTime(new Date());
        homeEquipment.setType(homeEquipment.getEquipBasicId());

        int count = homeEquipmentService.insertSelective(homeEquipment);
        return new HomeResult(HomeResultConstant.SUCCESS,count);
    }

    @ApiOperation(value = "修改设备")
    @RequiresPermissions("home:equipment:update")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
    public String update(@PathVariable("id") int id, ModelMap modelMap) {
        HomeEquipmentExample homeEquipmentExample = new HomeEquipmentExample();
        List<HomeEquipment> homeEquipments = homeEquipmentService.selectByExample(homeEquipmentExample);
        HomeEquipment homeEquipment = homeEquipmentService.selectByPrimaryKey(id);
        modelMap.put("homeEquipments", homeEquipments);
        modelMap.put("homeEquipment", homeEquipment);

        HomeRoomExample homeRoomExample = new HomeRoomExample();
        List<HomeRoom> homeRooms = homeRoomService.selectByExample(homeRoomExample);
        modelMap.put("homeRooms",homeRooms);

        HomeBaseDeviceExample homeBaseDeviceExample = new HomeBaseDeviceExample();
        List<HomeBaseDevice> homeBaseDevices = homeBaseDeviceService.selectByExample(homeBaseDeviceExample);
        modelMap.put("homeBaseDevices",homeBaseDevices);

        return "/manage/equipment/update.jsp";
    }

    @ApiOperation(value = "修改设备")
    @RequiresPermissions("home:equipment:update")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Object update(@PathVariable("id") int id, HomeEquipment homeEquipment) {
        homeEquipment.setId(id);
        homeEquipment.setLastTime(new Date());
        int count = homeEquipmentService.updateByPrimaryKeySelective(homeEquipment);
        return new HomeResult(HomeResultConstant.SUCCESS,count);
    }


    @ApiOperation(value = "删除设备")
    @RequiresPermissions("home:equipment:delete")
    @RequestMapping(value = "/delete/{ids}",method = RequestMethod.GET)
    @ResponseBody
    public Object delete(@PathVariable("ids") String ids) {
        int count = homeEquipmentService.deleteByPrimaryKeys(ids);
        return new HomeResult(HomeResultConstant.SUCCESS,count);
    }

}
